<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\grid\GridView;
use yii\helpers\Url;
$this->title = '自定义页面管理';
?>
<div class="container">
	<div class="row">
		<?php echo $this->render('_leftnav'); ?>
		<div class="col-md-10">
			<div class="page-header clearfix">
				<h1 class="pull-left">自定义页面管理</h1>
				<div class="pull-right">
					<button class="btn btn-success btn-sm pull-right" id="content-add-btn" type="button" data-target="#modal" data-toggle="modal"> 添加页面</button>
				</div>
			</div>
			<table class="table table-hover" id="content-table">
				<thead>
					<tr>
					<th>标题</th>
					<th>地址</th>
					<th>发布人</th>
					<th>是否显示</th>
					<th>发布时间</th>
					<th>操作</th>
					</tr>
				</thead>
				<tbody>
					<?php if ($data) { ?>
						<?php foreach ($data as $key => $val) { ?>
							<tr>
								<td>
									<a href="#" target="_blank"><?= Html::encode($val['page_title']) ?></a>
								</td>
								<td>
									<span class="text-muted text-sm">地址：<?= Html::encode($val['page_url']) ?></span>
								</td>
								<td><span class="text-success"><?= Html::encode($val['page_uname']) ?></span></td>
								<td>
									<?php 
										if($val['is_show']==1){
											echo "<font color='green'>显示</font>";
										}else{
											echo "<font color='red'>不显示</font>";
										}	
									?>
								</td>
								<td>
									<?= date("Y-m-d H:s:i",Html::encode($val['page_addtime']))?>
								</td>
								<td>
									<div class="btn-group">
										<a href="#modals" data-toggle="modal"  class="btn btn-default btn-sm UpdatePage" type="<?= Html::encode($val['page_id']) ?>">编辑</a>
										<a class="btn btn-default btn-sm DeletePage" type="<?= Html::encode($val['page_id']) ?>">删除</a>
									</div>
								</td>
							</tr>
						<?php } ?>
					<?php }else{ ?>
						<tr>
							<td colspan="20">
								<div class="empty">暂无记录</div>
							</td>
						</tr>
					<?php } ?>	
				</tbody>
			</table>
		</div>
	</div>
</div>
<div id="modal" class="modal fade">
	<div class="modal-dialog" style="width:900px;">
		<div class="modal-content" >
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
				<h4 class="modal-title">自定义页面</h4>
			</div>
			<div class="modal-body">
				<div id="type-creater-widget">
					<?php 
						$form = ActiveForm::begin([
							'options' => ['class' => 'form-horizontal',
							'id'=>'type-form','enctype'=>'multipart/form-data',
		'novalidate'=>'novalidate','data-widget-cid'=>'widget-9'],
							'action'=>Url::to(['operate/diyadd']),
							'method'=>'post',
							'fieldConfig' => [
								'template' => '<div class="form-group"><center><label class="col-md-2 control-label" for="type-name-field">{label}</label></center><div class="col-md-8 controls">{input}{error}</div></div>'
							], 
						]);
					?>
					<?= $form->field($model, 'page_title',['inputOptions'=>['placeholder'=>'请输标题']])->textInput() ?>

					<?= $form->field($model, 'page_url',['inputOptions'=>['placeholder'=>'请输url']])->textInput() ?>

					<?php $model->is_show=1; echo $form->field($model,'is_show')->radioList(['1'=>'显示','0'=>'不显示'],['separator'=>" ","style"=>"padding-top:6px;"]); ?>

					<?= $form->field($model, 'page_text')->textarea(['rows'=>36,'id' => 'my','class'=>'ckeditor']) ?>
				
				</div>
			</div>
			<div class="modal-footer">	
				<button type="button" class="btn btn-link" data-dismiss="modal" tabindex="6">取消</button>
				<?= Html::submitButton('保存', ['class' => 'btn btn-success','id'=>'category-create-btn']) ?>
			</div>
			<?php ActiveForm::end(); ?>
		</div>
	</div>
</div>
<div id="modals" class="modal fade"></div>
<?= Html::jsFile('/ueditor/ueditor.config.js')?>
<?= Html::jsFile('/ueditor/_examples/editor_api.js')?>
<script type="text/javascript">
	myeditor = Math.ceil(Math.random() * 25);
	$('.ckeditor').attr('id','my'+myeditor);
    var ue = UE.getEditor('my'+myeditor);
</script>
<script type="text/javascript">
	$(".DeletePage").click(function(){
		if(confirm("您确定要删除这条记录？")){
			var id=$(this).attr("type");
			$.ajax({
				type: "POST",
				url:"<?php echo Url::to(['operate/diydelete']);?>",
				data: "id="+id,
				dataType: 'json',
				success: function(msg){
					if (msg.code == 200) {
						history.go(0)
					}else if(msg.code == 400)
					{
						alert('删除失败')
					}else if(msg.code == 403) 
					{
						alert('暂无权限')
					}
				}
			});
		}
	})
	$(".UpdatePage").click(function(){
		var id=$(this).attr("type");
		$.ajax({
			type: "POST",
			url: "<?php echo Url::to(['operate/diyupdate']);?>",
			data: "id="+id+"&type=list",
			dataType: 'json',
			success: function(msg){
				if (msg.code == 200)
				{
					$("#modals").html(msg.data);
				}else if(msg.code == 403)
				{
					$("#modals").html('<div class="modal-dialog " style="width:170px;"><div class="modal-content"><div class="modal-header"><button aria-hidden="true" data-dismiss="modal" class="close" type="button">×</button><h4 class="modal-title"><font color="red">'+msg.msg+'</font></h4></div></div></div>');
				}
			}
		});
	})
</script>
